using System;
using System.Globalization;
using UnityEngine;

public class TimeUtility
{
    
    /// <summary>
    /// ����ת����
    /// </summary>
    /// <param name="time">2021-08-31</param>
    /// <returns>2021-07-24</returns>
    public static void SolarToLunar(int year,int month,int day,int hour,int min,out int cyear,out int cmonth,out int cday,out int chour)
    {   
        chour = Mathf.CeilToInt(hour / 2f) + 1;
        if (chour == 13)
        {
            chour = 1;
            if (((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day == 31)
                || ((year % 4 == 0) && month == 2 && day == 29)
                || ((year % 4 != 0) && month == 2 && day == 28)
                || ((month == 4 || month == 6 || month == 9 || month == 11) && day == 30))
            {
                if (month == 12)
                    month = 1;
                else 
                    month += 1;
                day = 1;
            }
            else
            {
                day += 1;
            }
        }
        ChineseLunisolarCalendar cncld = new ChineseLunisolarCalendar();
        DateTime dt = new DateTime(year,month,day,hour,min,0);
        cyear = cncld.GetSexagenaryYear(dt);//.GetYear(dt);
        
        cmonth = cncld.GetMonth(dt);
        
        int leapMonth = cncld.GetLeapMonth(cncld.GetYear(dt));
        bool isleap = false;
 
        if (leapMonth > 0)
        {
            if (leapMonth == cmonth)
            {
                //闰月
                isleap = true;
                cmonth--;
            }
            else if (cmonth > leapMonth)
            {
                cmonth--;
            }
        }
        //Debug.LogError($"isleap:{isleap} leapMonth:{leapMonth}  year:{year}  cyear:{cyear} ");
        cday = cncld.GetDayOfMonth(dt);
    }
    public static string LunarMonth2Name(int cMonth)
    {
        if (cMonth == 1) return "正月";
        if (cMonth == 2) return "二月";
        if (cMonth == 3) return "三月";
        if (cMonth == 4) return "四月";
        if (cMonth == 5) return "五月";
        if (cMonth == 6) return "六月";
        if (cMonth == 7) return "七月";
        if (cMonth == 8) return "八月";
        if (cMonth == 9) return "九月";
        if (cMonth == 10) return "十月";
        if (cMonth == 11) return "十一月";
        if (cMonth == 12) return "腊月";
        return cMonth.ToString();
    }
    
    public static string LunarYear2Name(int cYear)
    {
        if (cYear == 1) return "甲子";
        if (cYear ==02) return "乙丑";
        if (cYear ==03) return "丙寅";
        if (cYear ==04) return "丁卯";
        if (cYear ==05) return "戊辰";
        if (cYear ==06) return "己巳";
        if (cYear ==07) return "庚午";
        if (cYear ==08) return "辛未";
        if (cYear ==09) return "壬申";
        if (cYear ==10) return "癸酉";
        if (cYear ==11) return "甲戌";
        if (cYear ==12) return "乙亥";
        if (cYear ==13) return "丙子";
        if (cYear ==14) return "丁丑";
        if (cYear ==15) return "戊寅";
        if (cYear ==16) return "己卯";
        if (cYear ==17) return "庚辰";
        if (cYear ==18) return "辛巳";
        if (cYear ==19) return "壬午";
        if (cYear ==20) return "癸未";
        if (cYear ==21) return "甲申";
        if (cYear ==22) return "乙酉";
        if (cYear ==23) return "丙戌";
        if (cYear ==24) return "丁亥";
        if (cYear ==25) return "戊子";
        if (cYear ==26) return "己丑";
        if (cYear ==27) return "庚寅";
        if (cYear ==28) return "辛卯";
        if (cYear ==29) return "壬辰";
        if (cYear ==30) return "癸巳";
        if (cYear ==31) return "甲午";
        if (cYear ==32) return "乙未";
        if (cYear ==33) return "丙申";
        if (cYear ==34) return "丁酉";
        if (cYear ==35) return "戊戌";
        if (cYear ==36) return "己亥";
        if (cYear ==37) return "庚子";
        if (cYear ==38) return "辛丑";
        if (cYear ==39) return "壬寅";
        if (cYear ==40) return "癸卯";
        if (cYear ==41) return "甲辰";
        if (cYear ==42) return "乙巳";
        if (cYear ==43) return "丙午";
        if (cYear ==44) return "丁未";
        if (cYear ==45) return "戊申";
        if (cYear ==46) return "己酉";
        if (cYear ==47) return "庚戌";
        if (cYear ==48) return "辛亥";
        if (cYear ==49) return "壬子";
        if (cYear ==50) return "癸丑";
        if (cYear ==51) return "甲寅";
        if (cYear ==52) return "乙卯";
        if (cYear ==53) return "丙辰";
        if (cYear ==54) return "丁巳";
        if (cYear ==55) return "戊午";
        if (cYear ==56) return "己未";
        if (cYear ==57) return "庚申";
        if (cYear ==58) return "辛酉";
        if (cYear ==59) return "壬戌";
        if (cYear ==60) return "癸亥";
        return cYear.ToString();
    } 

    public static string LunarDay2Name(int cDay)
    { 
        if (cDay == 1) return "初一";
        if (cDay == 2) return "初二";
        if (cDay == 3) return "初三";
        if (cDay == 4) return "初四";
        if (cDay == 5) return "初五";
        if (cDay == 6) return "初六";
        if (cDay == 7) return "初七";
        if (cDay == 8) return "初八";
        if (cDay == 9) return "初九";
        if (cDay == 10) return "初十";
        if (cDay == 11) return "十一";
        if (cDay == 12) return "十二";
        if (cDay == 13) return "十三";
        if (cDay == 14) return "十四";
        if (cDay == 15) return "十五";
        if (cDay == 16) return "十六";
        if (cDay == 17) return "十七";
        if (cDay == 18) return "十八";
        if (cDay == 19) return "十九";
        if (cDay == 20) return "二十";
        if (cDay == 21) return "廿一";
        if (cDay == 22) return "廿二";
        if (cDay == 23) return "廿三";
        if (cDay == 24) return "廿四";
        if (cDay == 25) return "廿五";
        if (cDay == 26) return "廿六";
        if (cDay == 27) return "廿七";
        if (cDay == 28) return "廿八";
        if (cDay == 29) return "廿九";
        if (cDay == 30) return "三十";
        return cDay.ToString();
    }
    public static string LunarHour2Name(int cHour)
    {
        if (cHour == 1) return "子";
        if (cHour == 2) return "丑";
        if (cHour == 3) return "寅";
        if (cHour == 4) return "卯";
        if (cHour == 5) return "辰";
        if (cHour == 6) return "巳";
        if (cHour == 7) return "午";
        if (cHour == 8) return "未";
        if (cHour == 9) return "申";
        if (cHour == 10) return "酉";
        if (cHour == 11) return "戌";
        if (cHour == 12) return "亥";
        return cHour.ToString();
    }
}